/*
 * Copyright 2012 Marsrutai.lt
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package lt.marsrutai.mobile.android.activity;

import java.util.List;

import lt.marsrutai.mobile.android.R;
import lt.marsrutai.mobile.android.manager.AboutPreferenceManager;
import lt.marsrutai.mobile.android.manager.CachePreferenceManager;
import lt.marsrutai.mobile.android.manager.LanguagePreferenceManager;
import lt.marsrutai.mobile.android.manager.StopSearchPreferenceManager;
import android.os.Build;
import android.os.Bundle;

import com.actionbarsherlock.app.SherlockPreferenceActivity;

public class AppPreferenceActivity extends SherlockPreferenceActivity {

    private LanguagePreferenceManager languagePrefManager;

    @SuppressWarnings("deprecation")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {
            addPreferencesFromResource(R.xml.preferences_cache);
            addPreferencesFromResource(R.xml.preferences_stop_search);
            addPreferencesFromResource(R.xml.preferences_language);
            addPreferencesFromResource(R.xml.preferences_about);

            CachePreferenceManager cachePrefManager = new CachePreferenceManager(this, this);
            cachePrefManager.registerListeners();
            cachePrefManager.updateCurrentRequestCacheSize();

            StopSearchPreferenceManager stopSearchPrefManager = new StopSearchPreferenceManager(this, this);
            stopSearchPrefManager.registerListeners();
            stopSearchPrefManager.updateNearbyStopRadiusSumary();

            languagePrefManager = new LanguagePreferenceManager(this, this);
            languagePrefManager.updateCurrentAppLanguageSummary();
            languagePrefManager.updateCurrentSelection();

            new AboutPreferenceManager(this, this).registerListeners();
        }
    }

    @SuppressWarnings("deprecation")
    @Override
    protected void onResume() {
        super.onResume();

        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {
            languagePrefManager.registerListeners(getPreferenceScreen().getSharedPreferences());
        }
    }

    @SuppressWarnings("deprecation")
    @Override
    protected void onPause() {
        super.onPause();

        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {
            languagePrefManager.unregisterListeners(getPreferenceScreen().getSharedPreferences());
        }
    }

    @Override
    public void onBuildHeaders(List<Header> target) {
        loadHeadersFromResource(R.xml.preference_headers, target);
    }

}
